let register = {};
//默认验证码的值
register.checkCodes = '';
// 默认校验是否通过的标识
register.isAllpass = false;
register.isTelpass = false;
register.isCodepass = false;
register.isDragpass = false;
register.isAgreepass = false;

/*
@ desc   手机号校验
@ return 
@ date  2019/10/24
@author cyj
*/
register.checkTel = () => {
    $account = $('#phone_text');
    $account.blur((e) => {
        let tel = $(e.target).val();
        let reg = /^1[3578]\d{9}$/;
        if (!reg.test(tel)) {
            $('.phone_test').show();
            register.isTelpass = false;
        }
        else {
            // 前端验证通过
            $('.phone_test').hide('slow');
            register.isTelpass = true;
        }
    })
}

/*
@ desc   验证码校验
@ return 
@ date  2019/10/24
@author cyj
*/
register.checkCheckCode = () => {
    let get_code = $('.get_code');
    get_code.click((e) => {
        //先出发手机号校验，如果成功才去生成验证码
        // 触发手机号码校验事件
        $('#phone_text').blur();
        if (register.isTelpass) {
            setTimeout(() => {
                //前端模拟生成6位数验证码
                let str = "";
                for (let i = 0; i < 6; i++) {
                    str += parseInt(Math.random() * 10);
                }
                register.checkCodes = str;
                alert('【网易严选】验证码：' + str + ',请查收');
            }, 2000)

            let obj = $(e.target);
            let time = 30;
            let times = (obj) => {
                if (time == 0) {
                    // removeAttr从每一个匹配的元素中删除一个属性
                    // 利用input的disable属性，控制点击其无响应的特点，从而减少逻辑判断
                    obj.removeAttr("disabled");
                    obj.val("获取验证码");
                    time = 30;
                } else {
                    obj.attr("disabled", true);
                    obj.val(time + "s后重新发送");
                    time--;
                    setTimeout(function () {
                        times(obj)
                    }, 1000)
                }
            }
            times(obj);
        }

    })
    let input_code = $('#code');
    input_code.blur((e) => {
        let val = $(e.target).val();
        axios.post('192.168.2.143/wangyiyanxuan/utils/phoneCode', {
            params: {
                phoneCode: val,
                uuid: ""
            }
        })
            .then(function (response) {
                // 当验证码为空或者不正确时val != register.checkCodes || '' === $.trim(val)
                if (response.status === -1 ||  response.status === -2) {
                    $('.sliped_test').text('短信验证码格式错误').show();
                    register.isCodepass = false;
                }
                else {
                    // 触发手机号码校验事件
                    $('#phone_text').blur();
                    // 验证码输入正确
                    $('.sliped_test').hide();
                    register.isCodepass = true;
                }
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });

    })
}
/*
@ desc   注册事件
@ return 
@ date  2019/10/24
@author cyj
*/
register.registerEvent = (e) => {
    let regEvent = $('.faster_reg');
    regEvent.click((e) => {
        if (register.isCodepass && register.isTelpass && register.isAgreepass) {
            // 如果校验全部通过，跳转首页
            return regEvent.attr('href', './index.html')
        }
        return false;

    })

}
/*
@ desc   同意事件
@ return 
@ date  2019/10/24
@author cyj
*/
register.agreeEvent = () => {
    let agreeObj = $('.tick');
    agreeObj.click((e) => {
        // class开关
        $(e.target).toggleClass('on');
        // 判断是否含有指定的class
        if ($(e.target).hasClass('on')) {
            register.isAgreepass = true;
        }
        else {
            register.isAgreepass = false;
        }
    })
}
// 滑块验证
register.slidVerify = () => {
    var box = $('.slide_verify');
    var continer = $('.arrow');
    var isDown = false;
    var offsetX = 0;
    var boxOffsetX = box.offset().left;

    continer.on("mousedown", (event) => {
        var e = event || window.event;
        offsetX = e.offsetX;
        isDown = true;
    })

    box.on("mousemove", (event) => {
        var e = event || window.event;
        if (isDown) {
            var lefts = e.clientX - boxOffsetX - offsetX;
            if (lefts <= 0) {
                lefts = 0;
            } if (lefts >= box.width() - continer.width()) {
                lefts = box.width() - continer.width();

            }
            continer.css('left', lefts + "px");
            $('.slide_img2').css('left', lefts + "px");


        }
    })
    box.on("mouseup", (event) => {
        debugger
        isDown = false;
        var e = event || window.event;
        var lefts = e.clientX - boxOffsetX - offsetX;
        if (lefts >= 90 && lefts <= 94) {
            alert('验证通过');
        }
        else {
            continer.animate({ left: "0px" })
        }

    })
}

$(() => {
    // 调用校验手机号码
    register.checkTel();
    register.checkCheckCode();
    register.registerEvent();
    register.agreeEvent();
    register.slidVerify();

    // 注意：当验证码和输入手机必须是成对出现
    // 且一一对应校验通过后才能成功跳转，
    // 否则在验证码输入框失焦事件触发后
    //提示验证码与手机号不匹配

})